/**
 * 
 */
package com.bestbuy.daoimple;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.bestbuy.dao.CatalogDao;
import com.bestbuy.pojo.Catalog;
import com.bestbuy.pojo.Product;

/**
 * @author ThanhNgo
 * 
 */
@Repository
@SuppressWarnings({ "unchecked", "rawtypes" })
public class CatalogDaoImpl implements CatalogDao {

	@Autowired
	private SessionFactory sessionFactory;

	public List<Catalog> getListCatalog() {
		// TODO Auto-generated method stub
		Session session = sessionFactory.getCurrentSession();
		List list = session.createQuery("from Catalog").list();
		return list;
	}

	public Catalog getCatalogByCatalogID(String catalogid) {
		// TODO Auto-generated method stub
		Session session = sessionFactory.getCurrentSession();
		String hql = "from Catalog where idcatalog = ?";
		Query query = session.createQuery(hql);
		query.setString(0, catalogid);
		return (Catalog)query.uniqueResult();
	}

	/* (non-Javadoc)
	 * @see com.bestbuy.dao.CatalogDao#addCatalog(java.lang.String)
	 */
	public void addCatalog(Catalog catalog) {
		// TODO Auto-generated method stub
		Session session = sessionFactory.getCurrentSession();
		session.save(catalog);
	}

	/* (non-Javadoc)
	 * @see com.bestbuy.dao.CatalogDao#countCatalog()
	 */
	public int countCatalog() {
		Session session = sessionFactory.getCurrentSession();
		String sql = "select count(*) from Catalog";
		Query query = session.createQuery(sql);
		int count = ((Long) query.uniqueResult()).intValue();
		int page = count/5;
		int du = count%5;
		if(du>0)
		{
			page = page+1;
		}
		
		return page;
	}

	/* (non-Javadoc)
	 * @see com.bestbuy.dao.CatalogDao#getListCatalog(java.lang.String)
	 */
	public List<Catalog> getListCatalog(String page) {
		// TODO Auto-generated method stub
		Session session = sessionFactory.getCurrentSession();
		int temp = Integer.parseInt(page)-1;
		List<Catalog> catalogs ;
		String sql = "from Catalog";
		Query query = session.createQuery(sql);
		query.setFirstResult(temp*5);
		query.setMaxResults(5);
		catalogs = (List<Catalog>) query.list();
		return catalogs;
	}

	/* (non-Javadoc)
	 * @see com.bestbuy.dao.CatalogDao#updateCatalog(com.bestbuy.pojo.Catalog)
	 */
	public void updateCatalog(Catalog catalog) {
		// TODO Auto-generated method stub
		Session session = sessionFactory.getCurrentSession();
		session.update(catalog);
	}

	/* (non-Javadoc)
	 * @see com.bestbuy.dao.CatalogDao#deleteCatalog(com.bestbuy.pojo.Catalog)
	 */
	public void deleteCatalog(Catalog catalog) {
		// TODO Auto-generated method stub
		Session session = sessionFactory.getCurrentSession();
		session.update(catalog);
		for (Product product : catalog.getProducts()) {
			product.setIsdeleted(1);
			session.update(product);
		}
	}

	/* (non-Javadoc)
	 * @see com.bestbuy.dao.CatalogDao#getListCatalogByDeleted(java.lang.String)
	 */
	public List<Catalog> getListCatalogByDeleted(String isDeleted) {
		// TODO Auto-generated method stub
		Session session = sessionFactory.getCurrentSession();
		String sql = "from Catalog where isdeleted = ?";
		List<Catalog> catalogs ;
		Query query = session.createQuery(sql);
		query.setString(0, isDeleted);
		catalogs = (List<Catalog>) query.list();
		return catalogs;
	}

	/* (non-Javadoc)
	 * @see com.bestbuy.dao.CatalogDao#countCatalog(java.lang.String)
	 */
	public int countCatalog(String isDeleted) {
		Session session = sessionFactory.getCurrentSession();
		String sql = "select count(*) from Catalog where isdeleted = ?";
		Query query = session.createQuery(sql);
		query.setString(0, isDeleted);
		int count = ((Long) query.uniqueResult()).intValue();
		int page = count/5;
		int du = count%5;
		if(du>0)
		{
			page = page+1;
		}
		
		return page;
	}

	/* (non-Javadoc)
	 * @see com.bestbuy.dao.CatalogDao#getListCatalog(java.lang.String, java.lang.String)
	 */
	public List<Catalog> getListCatalog(String page, String isDeleted) {
		// TODO Auto-generated method stub
		Session session = sessionFactory.getCurrentSession();
		int temp = Integer.parseInt(page)-1;
		List<Catalog> catalogs ;
		String sql = "from Catalog where isdeleted = ?";
		Query query = session.createQuery(sql);
		query.setString(0, isDeleted);
		query.setFirstResult(temp*5);
		query.setMaxResults(5);
		catalogs = (List<Catalog>) query.list();
		return catalogs;
	}

}
